import java.util.Scanner;

/**
 * @author gao
 * @date 2025/7/2 22:29
 */

public class Solution {
    public boolean isHappy(int n) {
        int fast = HappyNum(n), slow = n;
        while (fast != slow) {
            fast = HappyNum(HappyNum(fast));
            slow = HappyNum(slow);
        }
        return slow == 1;
    }

    public int HappyNum(int n) {
        int sum = 0;
        while (n > 0) {
            int m = n % 10;
            sum += m * m;
            n /= 10;
        }
        return sum;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            Solution solution = new Solution();
            System.out.println(solution.isHappy(n));
        }
    }
}